<?php
namespace app\api\controller;

use think\Controller;
use think\Request;

class InstallPackage extends Controller
{
    public function getPackage(Request $request){
        $res = [];
        $type = $request->post('type');
        if($type !== 'ios' && $type !== 'android'){
            $res['status'] = 400;
            echo json_encode($res);die;
        }
        $arr = array();
        if($type == 'ios'){
            $data = scandir('../public/static/uploads/installationPackage/ios');
        }else{
            $data = scandir('../public/static/uploads/installationPackage/android');
        }

        foreach ($data as $value){
            if($value != '.' && $value != '..'){
                $arr[] = $value;
            }
        }
        $rs = $arr[count($arr)-1];


        if($rs){
            $size = get_headers('http://api.bjxlxd.com/static/uploads/installationPackage/'.$type.'/'.$rs,true);
            $size = round($size['Content-Length'] / 1024 * 100) / 100;
            $res['data']['code'] = 1;
            $res['data']['type'] = $type;
            $res['data']['UpdateStatus'] = 2;//0代表不更新，1代表有版本更新，不需要强制升级，2代表有版本更新，需要强制升级
            $res['data']['VersionCode'] = str_replace(strrchr($rs, "."),"",$rs);
            $res['data']['VersionName'] = $res['data']['VersionCode'];
            $res['data']['ModifyContent'] = '内容更新';
            $res['data']['DownloadUrl'] = 'http://api.bjxlxd.com/static/uploads/installationPackage/'.$type.'/'.$rs;
            $res['data']['ApkSize'] = $size;
            $res['data']['ApkMd5'] = md5($res['data']['VersionName']);
        }else{
            $res['data']['code'] = 0;
        }
        $res['status'] = 200;
        echo json_encode($res);
    }

}